package com.iknow.view.widget.media;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.genius.progress.RoundProgressBar;
import com.iknow.IKnow;
import com.iknow.file.CacheSystem;
import com.iknow.service.MusicPlayBinder;
import com.iknow.task.CommonTask.DownloadFileTask;
import com.iknow.task.GenericTask;
import com.iknow.task.TaskAdapter;
import com.iknow.task.TaskListener;
import com.iknow.task.TaskParams;
import com.iknow.task.TaskResult;

public class AudioView extends LinearLayout
{
  private View.OnClickListener PlayButtonClickListener;
  private Lyric lrc;
  private CommonTask.DownloadFileTask mAudioTask;
  private boolean mBinded;
  private MusicPlayBinder mBinder;
  private Context mContext;
  private IKAudioInfo mCurrentAudioInfo = null;
  private RelativeLayout mDownloadLayout;
  private String mLocalFileName;
  private Button mPlayButton;
  private PlayServiceConnection mPlayServiceConnection;
  private RoundProgressBar mProgressBar;
  private MusicplayReciver mReciver;
  private Intent mServiceIntent;
  private TaskListener mTaskListener;

  public AudioView(Context paramContext)
  {
    super(paramContext);
    this.lrc = null;
    this.mPlayServiceConnection = new PlayServiceConnection(this);
    this.mServiceIntent = new Intent("com.iknow.playservice");
    this.PlayButtonClickListener = new View.OnClickListener(this)
    {
      public void onClick()
      {
        if (AudioView.access$0(this.this$0).bConnected())
          if (AudioView.access$0(this.this$0).isPlaying())
            AudioView.access$0(this.this$0).pause();
        while (true)
        {
          do
            while (true)
            {
              while (true)
              {
                return;
                if (!(AudioView.access$0(this.this$0).bStoped()))
                  break;
                AudioView.access$0(this.this$0).play();
              }
              AudioView.access$2(this.this$0, IKnow.mCacheSystem.createFileNameByUrl(AudioView.access$1(this.this$0).getUrl()));
              if (!(IKnow.mCacheSystem.bCacheFileExist(AudioView.access$3(this.this$0))))
                break;
              AudioView.access$4(this.this$0);
            }
          while ((AudioView.access$5(this.this$0) != null) && (AudioView.access$5(this.this$0).getStatus() == AsyncTask.Status.RUNNING));
          AudioView.access$6(this.this$0).setVisibility(0);
          AudioView.access$7(this.this$0, new CommonTask.DownloadFileTask());
          AudioView.access$5(this.this$0).setListener(AudioView.access$8(this.this$0));
          TaskParams localTaskParams = new TaskParams();
          localTaskParams.put("url", AudioView.access$1(this.this$0).getUrl());
          localTaskParams.put("file_name", AudioView.access$3(this.this$0));
          CommonTask.DownloadFileTask localDownloadFileTask = AudioView.access$5(this.this$0);
          TaskParams[] arrayOfTaskParams = new TaskParams[1];
          arrayOfTaskParams[0] = localTaskParams;
          localDownloadFileTask.execute(arrayOfTaskParams);
          Toast.makeText(this.this$0.getContext(), this.this$0.getResources().getString(2131361952), 0).show();
        }
      }
    };
    this.mTaskListener = new TaskAdapter(this)
    {
      public String getName()
      {
        return "AudioTask";
      }

      public void onCancelled()
      {
        if ((paramGenericTask instanceof CommonTask.DownloadFileTask) && (AudioView.access$3(this.this$0) != null))
          IKnow.mCacheSystem.deleteFile(AudioView.access$3(this.this$0));
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        if (paramTaskResult == TaskResult.OK)
          AudioView.access$10(this.this$0);
        while (true)
        {
          do
            while (true)
            {
              return;
              if (paramTaskResult != TaskResult.READ_LOCAL)
                break;
              AudioView.access$10(this.this$0);
            }
          while (paramTaskResult != TaskResult.FAILED);
          AudioView.access$11(this.this$0, AudioView.access$5(this.this$0).getErrorMessage());
        }
      }

      public void onPreExecute()
      {
        AudioView.access$9(this.this$0);
      }

      public void onProgressUpdate(, Object paramObject)
      {
        try
        {
          AudioView.access$12(this.this$0).setProgress(((Integer)paramObject).intValue());
          return;
        }
        catch (Exception localException)
        {
          localException.printStackTrace();
        }
      }
    };
    this.mContext = paramContext;
    initView();
  }

  public AudioView(Context paramContext, AttributeSet paramAttributeSet)
  {
    super(paramContext, paramAttributeSet);
    this.lrc = null;
    this.mPlayServiceConnection = new PlayServiceConnection(this);
    this.mServiceIntent = new Intent("com.iknow.playservice");
    this.PlayButtonClickListener = new View.OnClickListener(this)
    {
      public void onClick()
      {
        if (AudioView.access$0(this.this$0).bConnected())
          if (AudioView.access$0(this.this$0).isPlaying())
            AudioView.access$0(this.this$0).pause();
        while (true)
        {
          do
            while (true)
            {
              while (true)
              {
                return;
                if (!(AudioView.access$0(this.this$0).bStoped()))
                  break;
                AudioView.access$0(this.this$0).play();
              }
              AudioView.access$2(this.this$0, IKnow.mCacheSystem.createFileNameByUrl(AudioView.access$1(this.this$0).getUrl()));
              if (!(IKnow.mCacheSystem.bCacheFileExist(AudioView.access$3(this.this$0))))
                break;
              AudioView.access$4(this.this$0);
            }
          while ((AudioView.access$5(this.this$0) != null) && (AudioView.access$5(this.this$0).getStatus() == AsyncTask.Status.RUNNING));
          AudioView.access$6(this.this$0).setVisibility(0);
          AudioView.access$7(this.this$0, new CommonTask.DownloadFileTask());
          AudioView.access$5(this.this$0).setListener(AudioView.access$8(this.this$0));
          TaskParams localTaskParams = new TaskParams();
          localTaskParams.put("url", AudioView.access$1(this.this$0).getUrl());
          localTaskParams.put("file_name", AudioView.access$3(this.this$0));
          CommonTask.DownloadFileTask localDownloadFileTask = AudioView.access$5(this.this$0);
          TaskParams[] arrayOfTaskParams = new TaskParams[1];
          arrayOfTaskParams[0] = localTaskParams;
          localDownloadFileTask.execute(arrayOfTaskParams);
          Toast.makeText(this.this$0.getContext(), this.this$0.getResources().getString(2131361952), 0).show();
        }
      }
    };
    this.mTaskListener = new TaskAdapter(this)
    {
      public String getName()
      {
        return "AudioTask";
      }

      public void onCancelled()
      {
        if ((paramGenericTask instanceof CommonTask.DownloadFileTask) && (AudioView.access$3(this.this$0) != null))
          IKnow.mCacheSystem.deleteFile(AudioView.access$3(this.this$0));
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        if (paramTaskResult == TaskResult.OK)
          AudioView.access$10(this.this$0);
        while (true)
        {
          do
            while (true)
            {
              return;
              if (paramTaskResult != TaskResult.READ_LOCAL)
                break;
              AudioView.access$10(this.this$0);
            }
          while (paramTaskResult != TaskResult.FAILED);
          AudioView.access$11(this.this$0, AudioView.access$5(this.this$0).getErrorMessage());
        }
      }

      public void onPreExecute()
      {
        AudioView.access$9(this.this$0);
      }

      public void onProgressUpdate(, Object paramObject)
      {
        try
        {
          AudioView.access$12(this.this$0).setProgress(((Integer)paramObject).intValue());
          return;
        }
        catch (Exception localException)
        {
          localException.printStackTrace();
        }
      }
    };
    this.mContext = paramContext;
    initView();
  }

  private void initView()
  {
    View localView = ((LayoutInflater)this.mContext.getSystemService("layout_inflater")).inflate(2130903041, null);
    this.mPlayButton = ((Button)localView.findViewById(2131296266));
    this.mPlayButton.setOnClickListener(this.PlayButtonClickListener);
    this.mProgressBar = ((RoundProgressBar)localView.findViewById(2131296268));
    this.mPlayButton.setClickable(false);
    this.mDownloadLayout = ((RelativeLayout)localView.findViewById(2131296267));
    addView(localView);
  }

  private void onBeginToDownload()
  {
    this.mPlayButton.setVisibility(8);
    this.mDownloadLayout.setVisibility(0);
  }

  private void onDownloadFailure(String paramString)
  {
    Toast.makeText(getContext(), paramString, 0).show();
    this.mDownloadLayout.setVisibility(8);
    this.mPlayButton.setVisibility(0);
  }

  private void onDownloadProgress(int paramInt)
  {
  }

  private void onDownloadSuccess()
  {
    this.mDownloadLayout.setVisibility(8);
    this.mPlayButton.setVisibility(0);
    play();
  }

  private void play()
  {
    if ((!(this.mBinded)) && (this.mBinder == null))
    {
      this.mServiceIntent.putExtra("audioUrl", this.mLocalFileName);
      this.mBinded = this.mContext.bindService(this.mServiceIntent, this.mPlayServiceConnection, 1);
    }
    while (true)
    {
      return;
      this.mPlayServiceConnection.play();
    }
  }

  private void stop()
  {
    uiFireOnPlayComplete();
  }

  private void uiFireOnPause()
  {
    Drawable localDrawable = getResources().getDrawable(2130837508);
    this.mPlayButton.setCompoundDrawablesWithIntrinsicBounds(null, localDrawable, null, null);
    this.mPlayButton.setCompoundDrawables(null, localDrawable, null, null);
    this.mPlayButton.setText("点击播放");
  }

  private void uiFireOnPlay()
  {
    Drawable localDrawable = getResources().getDrawable(2130837507);
    this.mPlayButton.setCompoundDrawablesWithIntrinsicBounds(null, localDrawable, null, null);
    this.mPlayButton.setCompoundDrawables(null, localDrawable, null, null);
    this.mPlayButton.setText("点击暂停");
  }

  private void uiFireOnPlayComplete()
  {
    Drawable localDrawable = getResources().getDrawable(2130837508);
    this.mPlayButton.setCompoundDrawablesWithIntrinsicBounds(null, localDrawable, null, null);
    this.mPlayButton.setCompoundDrawables(null, localDrawable, null, null);
    this.mPlayButton.setText("点击播放");
  }

  public void pause()
  {
    this.mPlayServiceConnection.pause();
  }

  public void setAudioInfo(IKAudioInfo paramIKAudioInfo)
  {
    this.mPlayButton.setClickable(true);
    Drawable localDrawable = getResources().getDrawable(2130837508);
    this.mPlayButton.setCompoundDrawablesWithIntrinsicBounds(null, localDrawable, null, null);
    this.mPlayButton.setCompoundDrawables(null, localDrawable, null, null);
    this.mPlayButton.setText("点击播放");
    this.mCurrentAudioInfo = paramIKAudioInfo;
  }

  public void startPlay()
  {
    if (this.mCurrentAudioInfo != null)
      play();
  }

  public void stopPlay()
  {
    if (this.mBinded)
    {
      this.mContext.unbindService(this.mPlayServiceConnection);
      this.mPlayServiceConnection.stop();
    }
    if ((this.mAudioTask != null) && (this.mAudioTask.getStatus() == AsyncTask.Status.RUNNING))
      this.mAudioTask.cancel(true);
  }

  public void stopService()
  {
    if (this.mBinded)
    {
      this.mContext.unregisterReceiver(this.mReciver);
      this.mPlayServiceConnection.release();
      this.mContext.stopService(this.mServiceIntent);
    }
  }

  private class MusicplayReciver extends BroadcastReceiver
  {
    public void onReceive(, Intent paramIntent)
    {
      int i;
      Bundle localBundle = paramIntent.getExtras();
      if (localBundle.containsKey("code"))
      {
        i = localBundle.getInt("code");
        if (-1 != i)
          break label37;
        AudioView.access$22(this.this$0);
      }
      while (true)
      {
        label37: 
        do
          return;
        while (i != 0);
        AudioView.access$21(this.this$0);
      }
    }
  }

  public class PlayServiceConnection
  implements ServiceConnection
  {
    private boolean mConnected = false;
    private boolean mIsStoped = false;

    public boolean bConnected()
    {
      return this.mConnected;
    }

    public boolean bStoped()
    {
      return this.mIsStoped;
    }

    public long getCurrentPosition()
    {
      Parcel localParcel = Parcel.obtain();
      try
      {
        AudioView.access$18(this.this$0).transact(MusicPlayBinder.CODE_GET_CURRENT_POISION, null, localParcel, 0);
        return localParcel.readInt();
      }
      catch (RemoteException localRemoteException)
      {
        localRemoteException.printStackTrace();
      }
    }

    public boolean isPlaying()
    {
      int i;
      if (AudioView.access$18(this.this$0) == null)
      {
        i = 0;
        return i;
      }
      Parcel localParcel = Parcel.obtain();
      try
      {
        AudioView.access$18(this.this$0).transact(MusicPlayBinder.CODE_IS_PLAYING, null, localParcel, 0);
        boolean[] arrayOfBoolean = new boolean[1];
        localParcel.readBooleanArray(arrayOfBoolean);
        i = arrayOfBoolean[0];
      }
      catch (RemoteException localRemoteException)
      {
        while (true)
          localRemoteException.printStackTrace();
      }
    }

    public void onServiceConnected(, IBinder paramIBinder)
    {
      AudioView.access$13(this.this$0, (MusicPlayBinder)paramIBinder);
      AudioView.access$14(this.this$0).startService(AudioView.access$15(this.this$0));
      AudioView.access$16(this.this$0, new AudioView.MusicplayReciver(this.this$0, null));
      IntentFilter localIntentFilter = new IntentFilter();
      localIntentFilter.addAction("com.iknow.playservice");
      AudioView.access$14(this.this$0).registerReceiver(AudioView.access$17(this.this$0), localIntentFilter);
      this.mConnected = true;
      play();
    }

    public void onServiceDisconnected()
    {
      AudioView.access$13(this.this$0, null);
      this.mConnected = false;
    }

    public void pause()
    {
      try
      {
        if (AudioView.access$18(this.this$0) == null)
          return;
        AudioView.access$1(this.this$0).setPlayTime(getCurrentPosition());
        AudioView.access$20(this.this$0);
        AudioView.access$18(this.this$0).transact(MusicPlayBinder.CODE_PAUSE, null, null, 0);
        this.mIsStoped = true;
      }
      catch (RemoteException localRemoteException)
      {
        localRemoteException.printStackTrace();
      }
    }

    public void play()
    {
      if (AudioView.access$18(this.this$0) == null)
        return;
      Parcel localParcel = Parcel.obtain();
      try
      {
        localParcel.writeLong(AudioView.access$1(this.this$0).getPlayTime());
        AudioView.access$18(this.this$0).transact(MusicPlayBinder.CODE_PLAY, localParcel, null, 0);
        this.mIsStoped = false;
        AudioView.access$19(this.this$0);
      }
      catch (RemoteException localRemoteException)
      {
        localRemoteException.printStackTrace();
      }
    }

    public void release()
    {
      try
      {
        if (AudioView.access$18(this.this$0) == null)
          return;
        AudioView.access$18(this.this$0).transact(MusicPlayBinder.CODE_RELEASE, null, null, 0);
        this.mIsStoped = true;
        AudioView.access$21(this.this$0);
      }
      catch (RemoteException localRemoteException)
      {
        localRemoteException.printStackTrace();
      }
    }

    public void seekTo()
    {
      Parcel localParcel = Parcel.obtain();
      try
      {
        localParcel.writeLong(paramLong);
        AudioView.access$18(this.this$0).transact(MusicPlayBinder.CODE_SEEK_TO, localParcel, null, 0);
        return;
      }
      catch (RemoteException localRemoteException)
      {
        localRemoteException.printStackTrace();
      }
    }

    public void stop()
    {
      try
      {
        if (AudioView.access$18(this.this$0) == null)
          return;
        AudioView.access$18(this.this$0).transact(MusicPlayBinder.CODE_STOP_PLAY, null, null, 0);
        this.mIsStoped = true;
        AudioView.access$21(this.this$0);
      }
      catch (RemoteException localRemoteException)
      {
        localRemoteException.printStackTrace();
      }
    }
  }
}